package com.vancouver.knows.ui.activity.userpost;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.ui.refresh.RefreshControllerView;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.FetchUserPostCategoriesTaskMark;
import com.vancouver.knows.business.task.mark.FetchUserPostsTaskMark;

/**
 * Created by Chris Li on 2016-07-13.
 */
public class UserPostPullRefreshView extends RefreshControllerView {

    public UserPostPullRefreshView(Context context) {
        super(context);
    }

    public void setUserPostRequestListener(UserPostListView.OnUserPostRequestListener listener){
        if (null != content){
            ((UserPostListView) content).userPostRequestListener = listener;
        }
    }

    @Override
    protected View createContentView() {
        return new UserPostListView(getContext(), this);
    }

    @Override
    public void onRefresh(ARefreshViewController controller) {
        ((UserPostListView) content).handleRefreshLoadItem();
        ((UserPostListView) content).showTopView();
    }

    public void loadData() {
        BusinessModule module = ((MainApplication) imContext).getBusinessModule();
        ATaskMark aTaskMark = module.getTaskMarkPool().getFetchUserPostsTaskMark();
        ((UserPostListView) content).initLoadableView(aTaskMark);
        ((UserPostListView) content).setSelectedTopCategory((FetchUserPostsTaskMark) aTaskMark);
    }

    public void loadFilteredData(String filter) {
        BusinessModule module = ((MainApplication) imContext).getBusinessModule();
        FetchUserPostsTaskMark aTaskMark = module.getTaskMarkPool().getFetchUserPostsTaskMark();
        aTaskMark.setFilter(filter);
        aTaskMark.reinitTaskMark();
        ((UserPostListView) content).clearItems();
        ((UserPostListView) content).initLoadableView(aTaskMark);
        ((UserPostListView) content).setSelectedTopCategory(aTaskMark);
        ((UserPostListView) content).scrollUp();
    }

    /*
    不同分类标签间，点击切换
     */
    public void switchFilteredData(String filter) {
        BusinessModule module = ((MainApplication) imContext).getBusinessModule();
        FetchUserPostsTaskMark aTaskMark = module.getTaskMarkPool().getFetchUserPostsTaskMark();
        aTaskMark.setFilter(filter);
        aTaskMark.reinitTaskMark();
        ((UserPostListView) content).clearItems();

        //((UserPostListView) content).initLoadableView(aTaskMark);会导致整个界面刷新（分类标签bar也看不到了）
        ((UserPostListView) content).setTaskMark(aTaskMark);

        ((UserPostListView) content).setSelectedTopCategory(aTaskMark);
        ((UserPostListView) content).scrollUp();
    }

    public void flush() {
        ((UserPostListView) content).notifyDataSetChanged();
    }
}
